package org.study.enums;

import lombok.Getter;

/**
 * 枚举类在实际项目中的应用示例：HTTP状态码枚举
 * 参考 https://blog.csdn.net/MeyrlNotFound/article/details/147870300
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2025年07月16日
 */
@Getter
public class HttpStatusEnumTest {
    public enum HttpStatus {
        OK(200, "OK"),
        BAD_REQUEST(400, "Bad Request"),
        UNAUTHORIZED(401, "Unauthorized"),
        FORBIDDEN(403, "Forbidden"),
        NOT_FOUND(404, "Not Found"),
        INTERNAL_SERVER_ERROR(500, "Internal Server Error");
        private final int code;
        private final String reason;

        HttpStatus(int code, String reason) {
            this.code = code;
            this.reason = reason;
        }

        public static HttpStatus fromCode(int code) {
            for (HttpStatus status : values()) {
                if (status.code == code) {
                    return status;
                }
            }
            throw new IllegalArgumentException("未知的HTTP状态码: " + code);
        }
    }
}
